<?php
/**
 * 权限处理器
 * 功能:增加新的权限
 *@author wolf  [Email: 116311316@qq.com]
 *@since 2011-07-20
 *@version 1.0
 */
abstract class NodeController extends Action {
	// 用户信息统一
	protected $_user_global;
	const COOKIENAME = 'user';
	protected $_group;
	
	/**
	 * 检验权限
	 */
	public function _routeDown() {
		$controller = $this->getControllerName (); // 当前控制器
		$action = $this->getActionName (); // 当前动作
		$islogin = false;
		//如果cookie存在 那么自动生成session
		if (isset ( $_COOKIE [self::COOKIENAME] )) {
			$userInfo = self::getMemberService ()->getOneMemberByUsername ( $_COOKIE [self::COOKIENAME] );
			$this->_group=self::getMemberService()->_group;
			$this->_user_global = $userInfo;
			$islogin = true;
			$this->view ()->assign ( 'user', $this->_user_global ); // 导入用户信息
		}
		
		// 排除无需权限方法  
		$allowAction = require 'authorize.php';
		
		//模块访问权限 
		

		if (! in_array ( $action, ($allowAction [$controller]) )) {
			
			if ($userInfo ['manager'] < 1 || $islogin == false) {
				$error = "需要管理员权限才能进入";
				$this->authorizeTemp ( $error );
				exit ();
			}
		
		}
		
		// 游客验证 登录之后 有注册会员 实名认证 管理员 和超级管理员之分 非管理管理员 都需要操作权限检验
		

		/* 检查被访问的页面 是否需要权限 */
		if ($action == 'c' || $action == 'i') {
			$category = CategoryModel::instance ()->getCateogryById ( $_REQUEST ['cid'] );
			
			$this->validteNode ( $category ['groupid'] );
		}
		
		if ($action == 'v') {
			// 内容权限大于分类权限 如果为7 就是继承分类权限 分类默认是游客
			$news = NewsModel::instance ()->getNewsWhere ( array ('id' => $_REQUEST ['id'] ) );
			if ($news ['groupid'] == 0) {
				$category = CategoryModel::instance ()->getCateogryById ( $news ['cid'] );
				$news ['groupid'] = $category ['groupid'];
			}
			
			$this->validteNode ( $news ['groupid'] );
		}
		
		$nodes = $this->getNodes ();
		$this->view ()->assign ( 'nodes', $nodes );
		
		//获取菜单 操作方法
		$this->view ()->assign ( 'action', $this->_actionName );
		$this->view ()->assign ( 'module', $this->_controllerName );
		
		//载入回复数
		$noreads = CommentModel::instance ()->getCommentNoRead ();
		$this->view ()->assign ( 'noreads', $noreads ['num'] );
	}
	
	private function getNodes() {
		$nodes = NodeModel::instance ()->getNodeAll ( array ('fid' => 0 ) );
		
		foreach ( $nodes as $k => $v ) {
			$rs = null;
			$rs = NodeModel::instance ()->getNodeAll ( array ('fid' => $v ['id'] ) );
			$nodes [$k] ['son'] = $rs;
		}
		return $nodes;
	
	}
	/**
	 *
	 * @param String $type
	 * LOGIN ADMIN VERIFY SUPER
	 */
	public function validteNode($type) {
		if ($type == 5) {
			$this->isLogin ();
		}
		if ($type == 3) {
			$this->isVerify ();
		}
		if ($type == 2) {
			$this->isAdmin ();
		}
		if ($type == 1) {
			$this->isSuperAdmin ();
		}
	}
	// 是否登录
	public function isLogin() {
		if (! isset ( $_COOKIE [self::COOKIENAME] )) {
			$error = '你尚未登录';
			$this->authorizeTemp ( $error );
		} else {
			return true;
		}
	}
	
	// 是否实名认证
	public function isVerify() {
		if ($this->_user_global ['verify'] < 1) {
			$error = '你的权限不够，未实名认证';
			
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	// 是否是管理员
	public function isAdmin() {
		if ($this->_user_global ['manager'] < 0) {
			$error = '你的权限不够，需要管理员权限';
			
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	// 是否是站长
	public function isSuperAdmin() {
		if ($this->_user_global ['manager'] == 1 || empty ( $this->_user_global ['groupid'] )) {
			$error = '你的权限不够，需要站长权限';
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	public function authorizeTemp($error) {
		$this->view ()->assign ( 'error', $error );
		$this->view ()->display ( 'file:public/error.tpl' );
	}
	
	/**
	 * 用户服务类
	 */
	public static function getMemberService() {
		return new MemberService ();
	}
	
	public static function getNewsService() {
		return new NewsService ();
	}

}